import 'dart:convert';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:du_application/http/HttpUtil.dart';
import 'package:du_application/http/Options.dart';
import 'package:du_application/model/AddOrderModel.dart';
import 'package:du_application/model/GoodsModel.dart';
import 'package:du_application/model/OrderModel.dart';
import 'package:du_application/provide/GoodsProvider.dart';
import 'package:du_application/routes/application.dart';
import 'package:du_application/units/iconfont.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provide/provide.dart';
import 'package:shared_preferences/shared_preferences.dart';

class DetailBuyBar extends StatelessWidget {
  final List<ProductList> productList;
  const DetailBuyBar({Key key,this.productList}) : super(key: key);

  @override
  Widget build(BuildContext context) {
     Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
addOrder(int goodsId,int productId) async {
 return   await _prefs.then((ins) async{
  Options options =  Options(ins.getString("token")??"");
 return  await HttpUtil.getInstance()
        .post("/cart/fastadd",data:{"goodsId":goodsId,"productId":productId,"number":1},
           options: options.options )
        .then((res) async {
          print(res);
          OrderModel  order =   OrderModel.fromJson(json.decode(res));
         return order;
    });
    });
  }   

    return Provide<GoodsProvider>(
        builder: (context, child,index){
  return Container( padding: EdgeInsets.all(ScreenUtil().setWidth(10)),
                          height: ScreenUtil().setHeight(1800),
                          color: Color.fromRGBO(248, 248, 250, 1),
                          child: Column(
                            children: <Widget>[
                              Stack(
                                children: <Widget>[
                                  Container(
                                    color: Colors.white,
                                    height: ScreenUtil().setHeight(250),
                                    child: Row(
                                      children: <Widget>[
                                        Container(
                                          width: ScreenUtil().setWidth(400),
                                          child: CachedNetworkImage( imageUrl:index.product.url,

                           fit:BoxFit.fill,
                           placeholder: (context, url) =>
                               Image.asset('assets/loading.jpg',fit:BoxFit.fitWidth,),
                           // print("111");
                         ),
                                        ),
                                        Container(
                                          padding: EdgeInsets.symmetric(
                                              vertical:
                                                  ScreenUtil().setHeight(40)),
                                          child: Column(
                                            mainAxisAlignment:
                                            
                                                MainAxisAlignment.spaceBetween,
                                                crossAxisAlignment: CrossAxisAlignment.start,
                                            children: <Widget>[
                                              Row(
                                                children: <Widget>[
                                                  Text(
                                                    "￥",
                                                    style: TextStyle(
                                                      color: Colors.black,
                                                      fontWeight:
                                                          FontWeight.bold,
                                                    ),
                                                  ),
                                                  Text(
                                                    index.product.price.toString(),
                                                    style: TextStyle(
                                                        color: Colors.black,
                                                        fontWeight:
                                                            FontWeight.bold,
                                                        fontSize: ScreenUtil()
                                                            .setSp(60)),
                                                  )
                                                ],
                                              ),
                                              Row(
                                                crossAxisAlignment:
                                                    CrossAxisAlignment.center,
                                                children: <Widget>[
                                                  Icon(IconFont.icon_duowei,
                                                      size: ScreenUtil()
                                                          .setSp(30)),
                                                  Text(index.product.specifications.join(""),
                                                      style: TextStyle(
                                                          color: Colors.grey,
                                                          fontWeight:
                                                              FontWeight.bold,
                                                          fontSize: ScreenUtil()
                                                              .setSp(30)))
                                                ],
                                              ),
                                            ],
                                          ),
                                        ),
                                      ],
                                    ),
                                  ),
                                  Positioned(
                                      right: ScreenUtil().setWidth(10),
                                      top: ScreenUtil().setWidth(10),
                                      child: IconButton(
                                        icon: Icon(Icons.close),
                                        onPressed: () {
                                          Navigator.of(context).pop();
                                        },
                                      ))
                                ],
                              ),
                             SizedBox(height:ScreenUtil().setWidth(10),),
                          Container(
                       
                            child: Expanded(
                              
                              child: GridView.count(
                                crossAxisCount: 4,
                                //纵轴间距
                                mainAxisSpacing: ScreenUtil().setWidth(10),
                                //横轴间距
                                crossAxisSpacing: ScreenUtil().setWidth(10),
                                //子组件宽高长度比例
                                childAspectRatio: 1.0,
                                children:productList.map((item){
                                  return  OutlineButton(
                                    padding: EdgeInsets.all(0),
                                    
                                    onPressed: (){
                                     index.selectProduct(item.id);
                                    },
                                    child:Container(
                                      width: 260,
                                   color: index.product.id==item.id?Colors.cyan[400]:Colors.white,
                                   child: Column(
                                     mainAxisAlignment: MainAxisAlignment.center,
                                     crossAxisAlignment: CrossAxisAlignment.center,
                                     children: <Widget>[
                                       Text(item.specifications.join(""),style:TextStyle(
                                         fontWeight: FontWeight.bold,
                                         fontSize: ScreenUtil().setSp(40),
                                         color: index.product.id==item.id?Colors.white:Colors.black
                                       )),
                                       Text("￥${item.price}",style:TextStyle(
                                         fontSize: ScreenUtil().setSp(30),
                                         color: index.product.id==item.id?Colors.white:Colors.black
                                       )
                                       )])));
                                     
                                      
                                }).toList() 
                              ),
                            ),
                            
                          ),
                          Container(
                            color: Colors.cyan[500],
                            height: ScreenUtil().setHeight(150),
                             child: FlatButton(
                               onPressed: () async {
                             OrderModel order=await addOrder(index.product.goodsId,index.product.id);
                               Application.router.navigateTo(context, "/submit?order=${Uri.encodeComponent(json.encode(order))}");
                                 },
                               child: Row(
                                 mainAxisAlignment: MainAxisAlignment.center,
                            children: <Widget>[
                              Row(
                                children: <Widget>[
                                  Text("￥",style: TextStyle(
                                color: Colors.white,
                                fontSize: ScreenUtil().setSp(40)
                              )),
                             Container(
                               margin: EdgeInsets.only(right: ScreenUtil().setSp(60)),
                               child: Text(index.product.price.toString(),style: TextStyle(
                                                             color: Colors.white,
                               fontSize: ScreenUtil().setSp(40)
                                                           ))
                             )
                                ],
                              ),
                              Text("|",style: TextStyle(
                                color: Colors.white
                              )),
                            Container(
                               margin: EdgeInsets.only(left: ScreenUtil().setSp(60)),
                              child: Text("立即购买",style: TextStyle(
                                color: Colors.white
                              ),)
                            )
                            ],
                               ),
                             ),
                           )
                           
                            ],
                          ),
                        );

        });  
         
  }
}